// import IFileClient from './file_client_interface'
import FileClient_Eslithe from './file_client_eslithe';
import FileClient_QN from './file_client_qn.js';
import FileClient_TX from './file_client_tx.js';
import FileClient_Ali from './file_client_ali.js';
// import { Message } from 'element-ui'
import { ElMessage } from 'element-plus';
// import { SyncPost } from '/@/api/linxyun/base/index.ts'
import { httpReqeustApi } from '/@/api/linxyun/base';

const httpReqeust = httpReqeustApi();

export default class FileIns {
	_getFileConfigUrl = 'genFileConfig';
	_fileClient = null;
	_isInit = false;
	static _instance = null;
	static async instance(params, isget, ifLoad) {
		if (this._instance === null || isget) {
			this._instance = await new FileIns();
		}
		await this._instance.init(params, ifLoad);
		return this._instance;
	}
	async getConfig() {
		if (!this._isInit) {
			console.error('[Error]p[File][getConfig]No init.');
			return null;
		}
		let res = await this._fileClient.getConfig();
		return res;
	}
	getFileFullUrl(fileID) {
		if (!this._isInit) {
			console.error('[Error]p[File][getFileFullUrl]No init.');
			return null;
		}
		return this._fileClient.getFileFullUrl(fileID);
	}
	// 文件列表，格式 name1: url1|name2: url2
	// 返回
	// isFullUrl === true  --> [{name: fileName, url: fileFullUrl}]
	// isFullUrl === false  --> [{name: fileName, url: fileUrl}]
	getFileList(fileStr, isFullUrl) {
		if (!this._isInit) {
			console.error('[Error][File][getFileList]No init.');
			return null;
		}
		return this._fileClient.getFileList(fileStr, isFullUrl);
	}

	// 返回full url
	async uploadContent(fileName, content, vm) {
		if (!this._isInit) {
			console.error('[Error]p[File][uploadContent]No init.');
			return null;
		}
		return await this._fileClient.uploadContent(fileName, content, vm);
	}
	// 删除文件,
	async deleteFile(url) {
		console.log('[Debug][File][deleteFile]Url: ', url);
		if (!this._isInit) {
			console.error('[Error]p[File][deleteFile]No init.');
			return false;
		}

		return await this._fileClient.deleteFile(url);
	}
	// 删除文件,
	async deleteFiles(fileStr) {
		if (!this._isInit) {
			console.error('[Error]p[File][deleteFiles]No init.');
			return;
		}
		let fileList = this.getFileList(fileStr, false);
		if (!fileList) {
			console.debug('[Debug][File][deleteFiles]Get file list fail.');
			return;
		}

		for (let i = 0; i < fileList.length; i++) {
			await this._fileClient.deleteFile(fileList[i].url);
		}
	}

	async init(params, ifLoad) {
		this._isInit = false;
		// if (this._fileClient === null || ifLoad) {
		console.log('初始化');
		await this.loadConfig(params);
		// }
		if (this._fileClient === null) {
			console.error('[Error][init]Load config fail.');
			return false;
		}
		let res = await this._fileClient.checkToken();
		if (res) {
			this._isInit = true;
		}
		return res;
	}
	async loadConfig(params) {
		let res;
		if (await localStorage.getItem('setting')) {
			res = {
				code: '0000',
				data: JSON.parse(localStorage.getItem('setting')),
				msg: 'success',
				success: true,
			};
		} else {
			res = await httpReqeust.SyncPost(this._getFileConfigUrl, { Bucket: params?.Bucket });
			await localStorage.setItem('setting', JSON.stringify(res.data));
		}
		if (!res || res.code !== '0000') {
			ElMessage({
				message: '没有配置文件服务器信息!',
				type: 'error',
				duration: BASE_API.MSG_SHOW_TIME,
			});
			console.error('[Error][File]Failed to get file server config.');
			return false;
		}
		// if (!res.Records || res.Records.length === 0) {
		// 	Message({
		// 		message: '没有配置文件服务器信息!',
		// 		type: 'error',
		// 		duration: BASE_API.MSG_SHOW_TIME
		// 	})
		// 	console.error('[Error][File]File server no config.')
		// 	return false
		// }
		let configData = res.data;
		params?.Bucket ? (configData.Bucket = params.Bucket) : '';

		this._fileClient = await this.newFileClient(configData);
		if (this._fileClient === null) {
			ElMessage({
				message: '文件服务器没有准备好，请联系管理员!',
				type: 'error',
				duration: BASE_API.MSG_SHOW_TIME,
			});
			return false;
		}
		return true;
	}
	async newFileClient(configData) {
		const FileType_Eslithe = 0;
		const FileType_QN = 1;
		const FileType_TX = 2;
		const FileType_Ali = 3;
		let fileClient = null;
		if (configData.FtpType === FileType_Eslithe) {
			fileClient = new FileClient_Eslithe(configData);
		} else if (configData.FtpType === FileType_QN) {
			fileClient = new FileClient_QN(configData);
		} else if (configData.FtpType === FileType_TX) {
			fileClient = new FileClient_TX(configData);
		} else if (configData.FtpType === FileType_Ali) {
			fileClient = new FileClient_Ali(configData);
		} else {
			console.log('[Error][File][newFileClient]The ftp type(', configData.FtpType, ') is invalid.');
		}

		return fileClient;
	}
}
